package com.atguigu.controller;

import com.atguigu.pojo.Article;
import com.atguigu.service.ArticleService;
import com.atguigu.util.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @program:weiblog-server
 * @author: 马雨清
 * @Time: 2023/8/21  11:09
 * @description:
 */
@RestController
@RequestMapping("/article")
@CrossOrigin
@Tag(name = "ArticleController")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @Operation(summary = "查询所有文章")
    @PostMapping("/findArticle")
    public Result<List<Article>> findArticle(@RequestBody(required = false) Article article){
        System.out.println("111");
        List<Article> articleList = articleService.findArticle(article);
        return Result.ok(articleList);
    }

    @Operation(summary = "根据uid查找")
    @GetMapping("findArticleByUid/{uid}")
    public Result<List<Article>> findArticleByUid(@PathVariable Integer uid){
        List<Article> articleList = articleService.findArticleByUid(uid);
        return Result.ok(articleList);
    }

    @Operation(summary = "根据id查找")
    @GetMapping("findArticleById/{id}")
    public Result<Article> fundArticleById(@PathVariable Integer id){
        Article article = articleService.findArticleById(id);
        return Result.ok(article);
    }

    @Operation(summary = "根据id修改文章")
    @PutMapping("updateArticle")
    public Result changeArticle(@RequestBody Article article){
        Integer result = articleService.updateArticle(article);
        if (result!=1){
            return Result.fail();
        }
        return Result.ok();
    }

    @Operation(summary = "添加文章")
    @PostMapping("insertArticle")
    public Result addArticle(@RequestBody Article article){
        if (article.getTitle()==null || article.getContent()==null ||article.getCid()==null){
            return Result.fail();
        }
        Integer result = articleService.addArticle(article);
        if (result!=1){
            return Result.fail();
        }
        return Result.ok();
    }

    @Operation(summary = "根据id删除文章")
    @DeleteMapping("deleteArticleById/{id}")
    public Result deleteArticleById(@PathVariable Integer id){
        Integer result = articleService.deleteArticleById(id);
        if (result!=1){
            return Result.fail();
        }
        return Result.ok();
    }

}
